package servletContext;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet1InitOnStartup extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ServletContext sc = getServletContext();
		String name = sc.getInitParameter("name");
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		out.print("name:"+name);
	}

	@Override
	// 比覆盖带参数的好
	// 带参的会调用不带参的，而且带参的已经有实现，不带参的为空方法，若覆盖则删除了实现
	// 使servlet2不访问servlet1而能访问数据
	// 在web.xml中配置load on startup
	public void init() throws ServletException {
		// 获得上下文
		ServletContext sc = getServletContext();
		// 向上下文中绑定数据
		sc.setAttribute("key", "lock");
	}

	// @Override
	// public void init(ServletConfig config) throws ServletException {
	// super.init(config);// 不省略
	// //获得上下文
	// ServletContext sc = getServletContext();
	// //向上下文中绑定数据
	// sc.setAttribute("key", "lock");
	// }
}
